﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace ThresholdAssistant.Tools
{
    /// <summary>
    /// 图片处理工具类
    /// </summary>
    public static class ImageUtil
    {
        /// <summary>
        /// 文字转图片
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="imgType">图片的后缀类型</param>
        /// <returns></returns>
        public static string ToImage(this string content, string imgType = "jpg")
        {
            //判断字符串不等于空和null
            if (content == null || content.Trim() == string.Empty)
                return string.Empty; ;

            string[] str = content.Split('\n');
            var length = str.Max(p => p.Length) + 10;

            var width = (int)Math.Ceiling(length * 18.0);
            width = width < 180 ? 180 : width;// 最小宽度 180

            var height = str.Length * 30;
            height = height < 180 ? 180 : height; //最小高度 180

            Bitmap image = new Bitmap(width, height);//创建一个位图对象
            Graphics g = Graphics.FromImage(image);//创建Graphics
            try
            {
                g.Clear(Color.White);//清空图片背景颜色
                Font font = new Font("Arial", 15.5f, (FontStyle.Bold));
                for (int index = 0; index < str.Length; index++)
                {
                    //如果用指定字体生成的文字像素宽度大于所在图片宽度 则换行显示
                    if (g.MeasureString(str[index], font).Width > image.Width)
                    {
                        //反推  获取当前行字符长度
                        int strLength = (int)Math.Ceiling((double)(image.Width / 18 - 10));
                        str[index] = str[index].NewLine((int)Math.Ceiling(str[index].Length * 0.9));
                    }
                }
                content = str.Aggregate((x, y) => x + "\n" + y);
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.DarkRed, 1.2f, true);
                g.DrawString(content, font, brush, 2, 2);

                //画图片的边框线e69da5e887aae799bee5baa631333330353533
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                var localImgUrl = Path.Combine(AppContext.BaseDirectory, "WordImages");
                if (!Directory.Exists(localImgUrl))
                {
                    Directory.CreateDirectory(localImgUrl);
                }
                localImgUrl += $@"/{Guid.NewGuid():N}.{imgType}";
                image.Save(localImgUrl);
                return localImgUrl;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

        /// <summary>
        /// 获取Image图片格式
        /// </summary>
        /// <param name="_img"></param>
        /// <param name="format"></param>
        /// <param name="contextType"></param>
        /// <returns></returns>
        public static ImageFormat GetImageFormat(this Bitmap _img, out string format, out string contextType)
        {
            if (_img.RawFormat.Equals(ImageFormat.Jpeg))
            {
                contextType = "image/jpeg";
                format = ".jpg";
                return ImageFormat.Jpeg;
            }
            if (_img.RawFormat.Equals(ImageFormat.Gif))
            {
                contextType = "image/gif";
                format = ".gif";
                return ImageFormat.Gif;
            }
            if (_img.RawFormat.Equals(ImageFormat.Png))
            {
                contextType = "image/png";
                format = ".png";
                return ImageFormat.Png;
            }
            if (_img.RawFormat.Equals(ImageFormat.Bmp))
            {
                contextType = "image/bmp";
                format = ".bmp";
                return ImageFormat.Bmp;
            }
            format = string.Empty;
            contextType = string.Empty;
            return null;
        }
    }
}